package com.zhu.solution.arrayQuestion;

import java.util.Arrays;

/*旋转图像
* 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像，这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出：[[7,4,1],[8,5,2],[9,6,3]]
* */

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2021/12/13/15:27
 * @Description:
 */
public class Solution_11 {

    public static void main(String[] args) {

        int[][] matirx = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};


        for (int i =0 ;i<matirx.length ;i++){
            System.out.println(Arrays.toString(matirx[i]));
        }

        new Solution_11().rotate(matirx);
        System.out.println("\n 变换后 \n");

        for (int i =0 ;i<matirx.length ;i++){
            System.out.println(Arrays.toString(matirx[i]));
        }

    }

    /*得到思路之后*/
    public void  rotate(int[][] matrix){
        int length = matrix.length;
        int temp = 0;
        int m = 0;
        int n = 0;
        for (int i = 0; i<length/2; i++){
            for (int j=i; j<length-i-1; j++){
                temp = matrix[i][j];
                m = length-i-1;
                n = length-j-1;

                matrix[i][j] = matrix[n][i];
                matrix[n][i] = matrix[m][n];
                matrix[m][n] = matrix[j][m];
                matrix[j][m] = temp;
            }
        }
    }

    //别人的算法
    public void rotate_2(int[][] matrix) {
        int length = matrix.length;
        //因为是对称的，只需要计算循环前半行即可
        for (int i = 0; i < length / 2; i++)
            for (int j = i; j < length - i - 1; j++) {
                int temp = matrix[i][j];
                int m = length - j - 1;
                int n = length - i - 1;
                matrix[i][j] = matrix[m][i];
                matrix[m][i] = matrix[n][m];
                matrix[n][m] = matrix[j][n];
                matrix[j][n] = temp;
            }
    }
}
